// LS123_BPA.cpp: implementation of the CLS123_BPA class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../../Include/Swi/LS123_BPA.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLS123_BPA::CLS123_BPA()
{
	m_chType = 'L';
	m_chSubType = 'S';

	m_chSign1 = ' ';
	memset(m_strBusName1,' ',8);
	m_strBusName1[8] = '\0';
	m_dkV1 = 0.0;

	m_chSign2 = ' ';
	memset(m_strBusName2,' ',8);
	m_strBusName2[8] = '\0';
	m_dkV2 = 0.0;

	m_chPar = ' ';
	strcpy(m_strMDE,"-1");
	m_strMDE[2] = '\0';
	m_dCycle = 0.0;
	m_dFaultR = 0.0;
	m_dFaultX = 0.0;
	m_dPercent = 0.0;
}

CLS123_BPA::CLS123_BPA(CLS123_BPA &ls123BPA)
{
	m_chType = ls123BPA.m_chType;
	m_chSubType = ls123BPA.m_chSubType;

	m_chSign1 = ls123BPA.m_chSign1;
	strcpy(m_strBusName1,ls123BPA.m_strBusName1);
	m_dkV1 = ls123BPA.m_dkV1;

	m_chSign2 = ls123BPA.m_chSign2;
	strcpy(m_strBusName2,ls123BPA.m_strBusName2);
	m_dkV2 = ls123BPA.m_dkV2;

	m_chPar = ls123BPA.m_chPar;
	strcpy(m_strMDE,ls123BPA.m_strMDE);
	m_dCycle = ls123BPA.m_dCycle;
	m_dFaultR = ls123BPA.m_dFaultR;
	m_dFaultX = ls123BPA.m_dFaultX;
	m_dPercent = ls123BPA.m_dPercent;
}


CLS123_BPA::~CLS123_BPA()
{

}

bool CLS123_BPA::FillEx(char *buffer)
{
	int i;

	if(EndOfSec(&buffer[0],1))
	{
		printf("Error in (CLS123_BPA: FillEx): no type!\n");
		return false;
	}
	m_chType = buffer[0];

	if(EndOfSec(&buffer[1],1))
	{
		printf("Error in (CLS123_BPA: FillEx): no subtype!\n");
		return false;
	}
	m_chSubType = buffer[1];

	if(EndOfSec(&buffer[2],2))
	{
		printf("Error in (CLN_BPA: FillEx): no sign 1!\n");
		return false;
	}
	m_chSign1 = buffer[3];

	for(i=0;i<8;i++)
	{
		if(!buffer[4+i])
			break;
		else
			m_strBusName1[i] = buffer[4+i];
	}
	m_strBusName1[i] = '\0';
	if(i!=8)
	{
		printf("Error in (CLS123_BPA: FillEx): no bus1 name!\n");
		return false;
	}

	m_dkV1 = StrToDouble(&buffer[12],4);
	if (m_dkV1 == 0)
	{
		printf("Error in (CLS123_BPA: FillEx): bus1 voltage is zero!\n");
		return false;
	}

	if(EndOfSec(&buffer[16],2))
	{
		printf("Error in (CLS123_BPA: FillEx): no sign 2!\n");
		return false;
	}
	m_chSign2 = buffer[17];

	for(i=0;i<8;i++)
	{
		if(!buffer[18+i])
			break;
		else
			m_strBusName2[i] = buffer[18+i];
	}
	m_strBusName2[i] = '\0';
	if(i!=8)
	{
		printf("Error in (CLS123_BPA: FillEx): no bus2 name!\n");
		return false;
	}

	m_dkV2 = StrToDouble(&buffer[26],4);
	if (m_dkV2 == 0)
	{
		printf("Error in (CLS123_BPA: FillEx): bus2 voltage is zero!\n");
		return false;
	}

	if(EndOfSec(&buffer[30],2))
		return true;
	m_chPar = buffer[31];

	if(EndOfSec(&buffer[32],5))
		return true;
	m_strMDE[0] = buffer[35];
	m_strMDE[1] = buffer[36];

	if(EndOfSec(&buffer[37],2))
		return true;

	m_dCycle = StrToDouble(&buffer[39],6);
	if(EndOfSec(&buffer[39],6))
		return true;

	m_dFaultR = StrToDouble(&buffer[45],6);
	if(EndOfSec(&buffer[45],6))
		return true;

	m_dFaultX = StrToDouble(&buffer[51],6);
	if(EndOfSec(&buffer[51],6))
		return true;

	m_dPercent = StrToDouble(&buffer[57],6);

	return true;
}

bool CLS123_BPA::UpdateEx(char *buffer)
{
	buffer[0] = m_chType;
	buffer[1] = m_chSubType;
	buffer[2] = ' ';

	buffer[3] = m_chSign1;
	strcpy(&buffer[4],m_strBusName1);
	DoubleToStr(m_dkV1,&buffer[12],4);
	buffer[16] = ' ';

	buffer[17] = m_chSign2;
	strcpy(&buffer[18],m_strBusName2);
	DoubleToStr(m_dkV2,&buffer[26],4);
	buffer[30] = ' ';

	buffer[31] = m_chPar;
	strcpy(&buffer[32],"   ");

	strcpy(&buffer[35],m_strMDE);
	strcpy(&buffer[37],"  ");

	DoubleToStr(m_dCycle,&buffer[39],6);

	DoubleToStr(m_dFaultR,&buffer[45],6);

	DoubleToStr(m_dFaultX,&buffer[51],6);

	DoubleToStr(m_dPercent,&buffer[57],6);

	return true;
}